//加载组件
layui.config({
    base: './js/'  //导入xm-select.js
}).extend({
    xmSelect: 'xm-select'
}).use(['xmSelect', 'layer','form'], function(){
    var xmSelect = layui.xmSelect,
        layer = layui.layer,
        form=layui.form;

    var demo2 = xmSelect.render({
        el: '#modxpostId',
        autoRow: true,
        tree: {
            show: true,//是否展示树状结构
            showFolderIcon: true,//是否开启三角图标
            showLine: true,//是否显示虚线
            indent: 20,//间距
            expandedKeys: [ 2 ],//默认展开的所有节点
        },
        height: 'auto',
        data:[]
    })
    var id = GetQueryString('postId');

    //部门下拉框
    $.ajax({
        url:"/logisticsSection/selectAll",
        dataType:'json',//数据类型
        type:'GET',//类型
        timeout:5000,//超时
        success:function(resp){
            for (let i = 0; i < resp.data.length; i++) {
                $("#sectionId").append("<option value="+resp.data[i].sectionId+">"+resp.data[i].sectionName+"</option>")
            }

            //角色信息回显

            if(id){
                let url ="/logisticsPost/selectId";
                let data ={postId:id};
                $.post(url,data,function (resp){
                    form.val('example',resp.data);
                    console.info($("#postId").val());
                })
            }
            form.render("select");
        }
    })



    //权限树回显
    $.ajax({
        url:"/logisticsModule/selectAll",
        data:{postId:id},
        dataType:'json',//数据类型
        type:'GET',//类型
        timeout:5000,//超时
        success:function(resp){
            demo2.update({
                data:resp.data,
                autoRow: true,
            })
        }
    })

      $("#up").on("click",function (){
          let formdata =new FormData(document.getElementById("myform"));
          $.ajax({
              url:"/logisticsModule/updatePost",
              data: {
                  sectionId:$("#sectionId").val(),
                  postName:$("#postName").val(),
                  postId:$("#postId").val(),
                  modIds:demo2.getValue('valueStr')},
              dataType:'json',//数据类型
              type:'POST',//类型
              timeout:5000,//超时
              success:function(resp){
                  parent.layer.msg(resp.mes);//父页面消息提示
                  layerClose();//关闭弹窗
              }
          })
      })
//关闭按钮
    $("#close_btn").on("click", function () {
        layerClose();
    });
})


//在子页面中关闭弹出层
function layerClose(){
    var index= parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
}



function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}